package com.example.contentprovider.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;

import com.example.contentprovider.db.UserDatabaseHelper;
import com.example.contentprovider.pojo.User;
import com.example.contentprovider.utils.Constants;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao{

    private final UserDatabaseHelper mUserDatabaseHelper;

    public UserDaoImpl(Context context){
        mUserDatabaseHelper = new UserDatabaseHelper(context);
    }

    @Override
    public long addUser(User user) {
        SQLiteDatabase db = mUserDatabaseHelper.getWritableDatabase();
        ContentValues values = user2Values(user);
        long result = db.insert(Constants.TABLE_NAME, null, values);
        db.close();
        return result;
    }

    @Override
    public int delUserById(int id) {
        SQLiteDatabase db = mUserDatabaseHelper.getReadableDatabase();
        int result = db.delete(Constants.TABLE_NAME, Constants.FIELD_ID, new String[]{id + ""});
        db.close();
        return result;
    }

    @Override
    public int updateUser(User user) {
        SQLiteDatabase db = mUserDatabaseHelper.getWritableDatabase();
        //根据id来更新内容
        ContentValues values = user2Values(user);
        int result = db.update(Constants.TABLE_NAME, values, Constants.FIELD_ID, new String[]{user.get_id() + ""});
        db.close();
        return result;
    }

    @NonNull
    private ContentValues user2Values(User user) {
        ContentValues values = new ContentValues();
        values.put(Constants.FIELD_USER_NAME, user.getUserName());
        values.put(Constants.FIELD_SEX, user.getSex());
        values.put(Constants.FIELD_PASSWORD, user.getPassword());
        values.put(Constants.FIELD_AGE, user.getAge());
        return values;
    }


    @Override
    public User getUserById(int id) {
        SQLiteDatabase db = mUserDatabaseHelper.getReadableDatabase();
        //可用sql语句
        String sql = "select * from " + Constants.TABLE_NAME + " where " + Constants.FIELD_ID + " = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{id + ""});
        User user = null;
        if(cursor.moveToNext()) {
            user = cursor2User(cursor);

        }
        db.close();
        return user;
    }

    //@SuppressLint("Range")
    private User cursor2User(Cursor cursor) {
        User user = new User();
        //
        int col_user_id = cursor.getColumnIndex(Constants.FIELD_ID);
        int col_user_name = cursor.getColumnIndex(Constants.FIELD_USER_NAME);
        int col_sex = cursor.getColumnIndex(Constants.FIELD_SEX);
        int col_password = cursor.getColumnIndex(Constants.FIELD_PASSWORD);
        int userId = cursor.getInt(col_user_id);
        int col_age = cursor.getColumnIndex(Constants.FIELD_AGE);
        user.set_id(userId);
        //
        String userName = cursor.getString(col_user_name);
        user.setUserName(userName);
        String userSex = cursor.getString(col_sex);
        user.setSex(userSex);
        String userPassword = cursor.getString(col_password);
        user.setPassword(userPassword);
        int userAge = cursor.getInt(col_age);
        user.setAge(userAge);

        return user;
    }

    @Override
    public List<User> listAllUser() {
        SQLiteDatabase db = mUserDatabaseHelper.getReadableDatabase();
        Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null,null);
        List<User> users = new ArrayList<>();
        while (cursor.moveToNext()) {
            User user = cursor2User(cursor);
            users.add(user);
        }
        db.close();
        return users;
    }
}



































